<?php
// 应用公共文件

// 构建表单
function build_form($fields, $default = null)
{
    $res = '';
    $btn_count = 0;
    if (empty($fields)) return;
    foreach ((array)$fields as $field) {
        $name = isset($field['name']) ? $field['name'] : '';
        $label = isset($field['label']) ? $field['label'] : '';
        $type = isset($field['type']) ? $field['type'] : 'text';
        $layui = isset($field['layui']) ? $field['layui'] : 'form-item';
        $class = isset($field['class']) ? $field['class'] : (in_array($type, ['textarea', 'editor', 'weditor', 'button', 'submit']) ? 'block' : ($layui === 'inline' ? 'block' : 'inline'));
        $style = isset($field['style']) && $field['style'] ? (in_array($type, ['editor', 'weditor']) ? (' style="display:none;' . $field['style'] . '"') : (' style="' . $field['style'] . '"')) : (in_array($type, ['editor', 'weditor']) ? ' style="display:none;"' : '');
        $value = isset($default[$name]) ? $default[$name] : (isset($default->$name) ? $default->$name : (isset($field['value']) ? $field['value'] : ($type === 'number' ? 0 : ($type === 'checkbox' ? (isset($field['options']) ? [] : '0') : ''))));
        $placeholder = isset($field['placeholder']) ? ' placeholder="' . $field['placeholder'] . '"' : '';
        $desc = isset($field['desc']) ? '<div class="' . ($type == 'textarea' ? 'layui-input-block ' : '') . 'layui-form-mid layui-word-aux">' . $field['desc'] . '</div>' : '';
        $step = isset($field['step']) ? ' step="' . $field['step'] . '"' : '';
        $min = isset($field['min']) ? ' min="' . $field['min'] . '"' : '';
        $max = isset($field['max']) ? ' max="' . $field['max'] . '"' : '';
        $rows = isset($field['rows']) ? ' rows="' . $field['rows'] . '"' : '';
        $autocomplete = isset($field['autocomplete']) ? ' autocomplete="' . $field['autocomplete'] . '"' : '';
        $sep = isset($field['sep']) ? $field['sep'] : '';
        $title = isset($field['title']) ? $field['title'] : '';
        $readonly = isset($field['readonly']) && $field['readonly'] ? ' readonly' : '';
        $disabled = isset($field['disabled']) && $field['disabled'] ? ' disabled' : '';
        $options = isset($field['options']) ? $field['options'] : [];
        // 自定义属性
        $properties = '';
        // 遍历自定义属性 lay-*
        foreach (array_intersect_key($field, array_flip(preg_grep('/^lay-.*?/', array_keys($field)))) as $prop_key => $prop_value) {
            $properties .= ' ' . $prop_key . '="' . $prop_value . '"';
        }
        // 遍历自定义属性 data-
        foreach (array_intersect_key($field, array_flip(preg_grep('/^data-.*?/', array_keys($field)))) as $prop_key => $prop_value) {
            $properties .= ' ' . $prop_key . '="' . $prop_value . '"';
        }
        if ($type === 'hidden') {
            $res .= '<input type="' . $type . '" name="' . $name . '" value="' . $value . '"' . $properties . '>';
        } elseif ($type === 'number') {
            $res .= '<div class="layui-' . $layui . '">
                    <label class="layui-form-label">' . $label . '</label>
                    <div class="layui-input-' . $class . '">
                        <input type="number" name="' .  $name . '" value="' .  $value . '"' .  $step . $min . $max . $placeholder . $autocomplete . $style . $readonly . $disabled . $properties . ' class="layui-input">
                    </div>' .  $desc . '
                </div>';
        } elseif ($type === 'select') {
            $res .= '<div class="layui-' . $layui . '">
                    <label class="layui-form-label">' .  $label . '</label>
                    <div class="layui-input-' .  $class . '">
                        <select name="' .  $name . '"' . $style . $disabled . $properties . '>';
            foreach ($options as $k => $v) {
                $res .= '<option value="' .  $k . '"' . ((string)$k === (string)$value ? ' selected' : '') . '>' .  $v . '</option>';
            }
            $res .= '</select>
                    </div>' .  $desc . '
                </div>';
        } elseif ($type === 'radio') {
            $res .= '<div class="layui-' . $layui . '">
                    <label class="layui-form-label">' .  $label . '</label>
                    <div class="layui-input-' .  $class . '">';
            foreach ($options as $k => $v) {
                $res .= '<input type="radio" name="' .  $name . '"' . ((string)$k === (string)$value ? ' checked' : '') . ' value="' .  $k . '" title="' .  $v . '">';
            }
            $res .= '</div>' .  $desc . '
                </div>';
        } elseif ($type === 'switch') {
            $res .= '<div class="layui-' . $layui . '">
                <label class="layui-form-label">' .  $label . '</label>
                <div class="layui-input-' .  $class . '">
                <input type="hidden" name="' . $name . '" value="0">
                <input type="checkbox" name="' . $name . '" value="1"' . ((string)$value === '1' ? ' checked' : '') . ' lay-text="' . (isset($field['lay-text']) ? $field['lay-text'] : 'ON|OFF') . '" lay-skin="switch">
                </div>
            </div>';
        } elseif ($type === 'switch_r') {
            $res .= '<div class="layui-' . $layui . '">
                <label class="layui-form-label">' .  $label . '</label>
                <div class="layui-input-' .  $class . '">
                <input type="hidden" name="' . $name . '" value="1">
                <input type="checkbox" name="' . $name . '" value="0"' . ((string)$value === '0' ? ' checked' : '') . ' lay-text="' . (isset($field['lay-text']) ? $field['lay-text'] : 'ON|OFF') . '" lay-skin="switch">
                </div>
            </div>';
        } elseif ($type === 'checkbox') {
            $res .= '<div class="layui-' . $layui . '">
                    <label class="layui-form-label">' . $label . '</label>
                    <div class="layui-input-' . $class . '">';
            if ($options) {
                foreach ($options as $k => $v) {
                    $res .= '<input type="checkbox" name="' . $name . '[]" value="' . $k . '" lay-skin="' . (isset($field['lay-skin']) ? $field['lay-skin'] : 'primary') . '" title="' . $v . '"' . (in_array($k, (array)$value) ? ' checked' : '') . '>';
                    $res .= $k === count($options) ? '' : $sep;
                }
            } else {
                $res .= '<input type="hidden" name="' . $name . '" value="0">';
                $res .= '<input type="checkbox" name="' . $name . '" value="1" lay-skin="' . (isset($field['lay-skin']) ? $field['lay-skin'] : 'primary') . '" title="' . ($title ?: '开启') . '"' . ('1' === (string)$value ? ' checked' : '') . '>';
            }
            $res .= '</div>' . $desc . '
                </div>';
        } elseif ($type === 'textarea') {
            $res .= '<div class="layui-' . $layui . ' layui-form-text">
                    <label class="layui-form-label">' . $label . '</label>
                    <div class="layui-input-' . $class . '">
                        <textarea name="' . $name . '"' . $placeholder . $rows . $style . $readonly . $disabled . $properties . ' class="layui-textarea">' . $value . '</textarea>
                    </div>
                </div>';
        } elseif ($type === 'upload') {
            $res .= '<div class="layui-' . $layui . '">
                    <label class="layui-form-label">' . $label . '</label>
                    <div class="layui-input-' . $class . '">
                        <input type="text" name="' . $name . '" value="' . $value . '"' . $placeholder . $autocomplete . $style . $readonly . $disabled . $properties . ' class="layui-input upload">
                    </div>
                </div>';
        } elseif (in_array($type, ['editor', 'weditor'])) {
            $res .= '<div class="layui-' . $layui . ' layui-form-text">
                    <label class="layui-form-label">' . $label . '</label>
                    <div class="layui-input-' . $class . '">
                        <textarea name="' . $name . '" class="' . $type . ' layui-textarea"' . $style . $properties . '>' . $value . '</textarea>
                    </div>
                </div>';
        } elseif ($type === 'html') {
            $res .= '<div class="layui-' . $layui . '">
                    <label class="layui-form-label">' . $label . '</label>
                    <div class="layui-input-' . $class . '">
                        ' . $value . '
                    </div>' . $desc . '
                </div>';
        } elseif ($type === 'date') {
            $res .= '<div class="layui-' . $layui . '">
                    <label class="layui-form-label">' . $label . '</label>
                    <div class="layui-input-' . $class . '">
                        <input type="text" name="' .  $name . '" value="' .  $value . '"' . $placeholder . $autocomplete . $style . $readonly . $disabled . $properties . ' class="date layui-input">
                    </div>' .  $desc . '
                </div>';
        } elseif ($type === 'color') {
            $res .= '<div class="layui-' . $layui . '">
                    <label class="layui-form-label">' . $label . '</label>
                    <div class="layui-input-' . $class . '">
                        <input type="text" name="' .  $name . '" value="' .  $value . '"' . $placeholder . $autocomplete . $style . $readonly . $disabled . $properties . ' class="color layui-input">
                    </div>' .  $desc . '
                </div>';
        } elseif (in_array($type, ['submit', 'reset', 'button'])) {
            $btn_count++;
            if ($btn_count === 1) {
                $res .= '<div class="layui-' . $layui . '">';
                $res .= ($layui !== 'inline') ? '<div class="layui-input-' . $class . '">' : '';
            }
            $res .= '<button type="' . $type . '"' . $style . $disabled . ' class="layui-btn' . (isset($field['btn']) ? ' layui-btn-' . $field['btn'] : '') . $properties . '">' . $value . '</button> ';
            if ($btn_count === count(array_filter($fields, function ($item) {
                return isset($item['type']) && in_array($item['type'], ['submit', 'reset', 'button']);
            }))) {
                $res .= ($layui !== 'inline') ? '</div>' : '';
                $res .= $desc;
                $res .= '</div>';
            }
        } else {
            $res .= '<div class="layui-' . $layui . '">
                    <label class="layui-form-label">' . $label . '</label>
                    <div class="layui-input-' . $class . '">
                        <input type="' . $type . '" name="' . $name . '" value="' . $value . '"' . $placeholder . $autocomplete . $style . $readonly . $disabled . $properties . ' class="layui-input">
                    </div>' . $desc . '
                </div>';
        }
    }
    return $res;
}
